package com.tyf.tool;

import memServer.business.BusinessServer;

/**  GPS计算公共类
 * @author 周广文:15146639681
 *
 * 2016年5月12日
 */
public class GpsUtil {
	
	/**
	 * 计算弧度
	 * @param d
	 * @return
	 */
	public static double rad(double d){
		return d*Math.PI/180.0;
	}
	
	
	
	public static double distance(double lat1,double lng1,double lat2,double lng2){
		float EARTH_RADIUS = 6378.137f;
		double radLat1 = rad(lat1);
		double radLat2 = rad(lat2);
		double a = radLat1 - radLat2;
		double b = rad(lng1) - rad(lng2);
		double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) + Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
		s = s * EARTH_RADIUS;
		s = (int)(s * 10000000) / 10000;
		return s;
	}
	
	public static void main(String[] args) {
		System.out.println(GpsUtil.distance(39.790281,116.453916,35.290423,108.887419));
		System.out.println(GpsUtil.distance(31.232110,121.550634,32.828196,106.256637));
	}
	/**
	 * 得到GPS点
	 * @param obdId
	 * @return
	 */
	public static GpsVo getGpsByObdId(String obdId) {
		GpsVo obd = new GpsVo();
		String temp = "test_odb_" + obdId + "_2c";
		String obd_lng = BusinessServer.getInstance().getHKeyValue(temp, "lng");
		String obd_lat = BusinessServer.getInstance().getHKeyValue(temp, "lat");
		String vin_code = BusinessServer.getInstance().getHKeyValue(temp, "vinCode");
		obd.setLat(obd_lat);
		obd.setLng(obd_lng);
		if (null != vin_code && !vin_code.equals(""))
			obd.setVin(vin_code);
		else
			obd.setVin(obdId);
		obd.setObdId(obdId);
		return obd;
	}
}
